package ModelLayer;
import java.util.HashMap;

/**
 * Class for objects of ProductCollection
 * 
 * @author (AF, RA, KP, RL) 
 * @version (2012.06.05)
 */
public class ProductCollection
{
    private static ProductCollection instance;
    private HashMap<Integer, Product> products;
    
    /**
     * Constructor for objects of class Product
     */
    private ProductCollection()
    {
        products = new HashMap<Integer, Product>();
    }
    
    /**
     * implementation of singleton pattern
     * 
     * @param none
     * 
     * @return instance of object of type ProductCollection only if it does not exist
     */
    public static ProductCollection getInstance()
    {
        if (instance == null) {
            instance = new ProductCollection();
        }
        
        return instance;    
    }

    /**
     * add object of type Product to ProductCollection
     * 
     * @param barcode barcode of product as int
     * @param product product to add as Product
     * 
     * @return none
     */
    public void addProduct(int barcode, Product product)
    {
        Integer bar = (Integer) barcode;
        products.put(bar, product);
    }
    
    /**
     * add object of type ProductItem to instance of Product
     * 
     * @param product product to add instance of ProductItem to as Product
     * @param productItem productItem to add to instance of Product as ProductItem
     * 
     * @return none
     */
    public void addProductItem(Product product, ProductItem productItem)
    {
        product.addProductItem(productItem);
    }
    
    /**
     * get object of type Product from barcode
     * 
     * @param barcode barcode of product as String
     * 
     * @return object product of type Product
     */
    public Product findProduct(int barcode) 
    {
        Integer bar = (Integer) barcode;
        Product product = products.get(bar);
        return product;
    }
    
    /**
     * delete object product of type Product from barcode
     * 
     * @param barcode barcode of product as int
     * 
     * @return none
     */
    public boolean deleteProduct(int barcode) 
    {
        Integer bar = (Integer) barcode;
        Product product = products.get(bar);
        if (product != null) {
            products.remove(bar);
            return true;
        } else {
            return false;
        }
    }
    
    /**
     * get productCollection as HashMap
     * 
     * @return all products as HashMap
     */
    public HashMap<Integer, Product> listAllProducts()
    {
        return products;
    }
}
